package data;

import java.util.ArrayList;

import models.AutoSite;
import models.Blog;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class BlogData {
	private Objectify ofy;
	
	public BlogData() {
		try {
			ObjectifyService.register(Blog.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public void insert(Blog blog) {
		ofy.put(blog);
	}
	
	public Blog getById(String blogId) {
		Blog blog  = new Blog();
		try {
			blog = ofy.get(new Key<Blog>(Blog.class, blogId));
		} catch (Exception e) {
			blog = null;
		}
		return blog;
	}
	
	public ArrayList<Blog> getAllByGmail(String gmail) {
		ArrayList<Blog> blogs = new ArrayList<Blog>();
		Query<Blog> q = ofy.query(Blog.class).filter("gmail", gmail);
		for (Blog blog: q) {
			blogs.add(blog);
		}
		return blogs;
	}
	
	public void delete(Blog blog) {
		ofy.delete(blog);
	}
}
